package m1;


import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //建立链接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        //默认就是5672，可以省略
        f.setPort(5672);

        f.setUsername("admin");
        f.setPassword("admin");

        Channel c = f.newConnection().createChannel();

        //定义队列，要求服务器先把队列准备好
        c.queueDeclare("helloworld",false,false,false,null);

        DeliverCallback deliverCallback = new DeliverCallback(){
            @Override
            public void handle(String s, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String msg = new String(body);
                System.out.println("收到："+ msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback(){
            @Override
            public void handle(String s) throws IOException {

            }
        };

        //从队列接收数据（消费数据）
        //deliverCallback   用来处理消息的回调对象
        //cancelCallback    取消消息处理时的回调对象
        c.basicConsume("helloworld",true,deliverCallback,cancelCallback);

    }
}
